package com.android.yinghebike.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.android.baselibrary.interface_.CommCallBack;
import com.android.baselibrary.tool.Log;
import com.android.baselibrary.util.DateUtil;
import com.android.yinghebike.R;
import com.android.yinghebike.utils.Utils;
import com.mapbox.api.directions.v5.models.DirectionsRoute;

import java.util.List;

import cn.wandersnail.commons.util.DateUtils;

/**
 * Created by Administrator on 2018/8/22.
 */

public class Nav_RouterListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    Context mContext;
    List<DirectionsRoute> list;
    CommCallBack callBack;
    int select = 0;

    public Nav_RouterListAdapter(Context mContext, CommCallBack callBack) {
        this.mContext = mContext;
        this.callBack = callBack;
    }

    public void setData(List<DirectionsRoute> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    public void setSelect(DirectionsRoute selectRouter)
    {
        for(int i = 0 ;i< list.size();i++)
        {
            DirectionsRoute route = list.get(i);
            if(route.routeIndex()==selectRouter.routeIndex())
            {
                select = i ;
            }
        }
       notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater mInflater = LayoutInflater.from(mContext);
        ViewGroup viewGroup = (ViewGroup) mInflater.inflate(R.layout.item_routerlist, null, false);
        ContentViewHolder holder = new ContentViewHolder(viewGroup);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ContentViewHolder viewHolder = (ContentViewHolder) holder;
        final DirectionsRoute info = list.get(position);
        //数据绑定逻辑
        try {
            Log.i("info.duration():"+ info.duration());
            viewHolder.tv_num.setText("方案" + (position + 1));

            viewHolder.tv_distance.setText(Utils.getDistance(info.distance()));
            viewHolder.tv_time.setText(DateUtil.generateTime(info.duration().intValue()));
            if (select == position) {
                viewHolder.itemView.setBackgroundResource(R.drawable.shape_round16dp_1e232d);
                viewHolder.tv_num.setTextColor(Color.parseColor("#cccccc"));
                viewHolder.tv_distance.setTextColor(Color.parseColor("#ffffff"));
                viewHolder.tv_time.setTextColor(Color.parseColor("#dddddd"));
            } else {
                viewHolder.itemView.setBackgroundResource(R.drawable.shape_round16dp_stroke_1e232d);
                viewHolder.tv_num.setTextColor(Color.parseColor("#333333"));
                viewHolder.tv_distance.setTextColor(Color.parseColor("#111111"));
                viewHolder.tv_time.setTextColor(Color.parseColor("#555555"));
            }

            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (callBack != null) {
                        callBack.onResult(info);
                    }
                }
            });
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }


    class ContentViewHolder extends RecyclerView.ViewHolder {
        TextView tv_distance, tv_time,tv_num;

        public ContentViewHolder(View itemView) {
            super(itemView);
            tv_distance = itemView.findViewById(R.id.tv_distance);
            tv_time = itemView.findViewById(R.id.tv_time);
            tv_num= itemView.findViewById(R.id.tv_num);
        }
    }
}
